package cn.service;

import cn.dao.UserDao;
import cn.entity.User;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    @Qualifier("userdao")
    private UserDao userDao;
    @Override
    public int getUserTotle() {
        return  userDao.userTotle();
    }

    public Page getUserList(Map<String,Object> cond){
        Page page = new Page();
        //总共的信息条数
        page.setTotal(userDao.userTotle());
        //当前页码
        int curPage = Integer.parseInt(String.valueOf(cond.get("curPage")));
        //拿到每页的长度
        int perTotle = (int)cond.get("perTotle");
        //总共有多少页
        page.setPageNum(userDao.userTotle()/perTotle+userDao.userTotle()%perTotle==0?0:1);

        page = PageHelper.startPage(curPage,perTotle);
        List<User> userList =  userDao.userList(cond);
        return   page;
    }

    @Override
    public boolean addUser(User user) {
       boolean b = userDao.addUser(user);
       return b;
    }
}
